﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;

namespace EntityServices
{
    /// <summary>
    /// Summary description for DonPhucKhao
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class DonPhucKhao : System.Web.Services.WebService
    {
        [WebMethod]
        public bool DangKyPhucKhao(string maSoSinhVien, string hinhThucPhucKhao, DateTime ngayPhucKhao, int hocKy, int namHoc, string maLopHoc)
        {
            try
            {
                HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();

                int sinhVienID = 0;
                foreach (SINH_VIEN sv in db.SINH_VIENs)
                {
                    if (sv.MASOSINHVIEN == maSoSinhVien)
                    {
                        sinhVienID = sv.SINHVIENID;
                    }
                }

                int lopHocID = 0;
                foreach (LOP_HOC lh in db.LOP_HOCs)
                {
                    if (lh.MALOPHOC == maLopHoc)
                    {
                        lopHocID = lh.LOPHOCID;
                    }
                }
                DON_PHUC_KHAO don = new DON_PHUC_KHAO { SINHVIENID = sinhVienID, HINHTHUCPHUCKHAO = hinhThucPhucKhao, NGAYPHUCKHAO = ngayPhucKhao, HOCKY = hocKy, NAM = namHoc, LOPHOCID = lopHocID };
                db.DON_PHUC_KHAOs.InsertOnSubmit(don);
                db.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public List<DON_PHUC_KHAO> LayDanhSachDonPhucKhao(int hocKy, int nam, string maLopHoc, string hinhThucPhucKhao)
        {
            List<DON_PHUC_KHAO> ds = new List<DON_PHUC_KHAO>();
            HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();

            int lopHocID = 0;
            foreach (LOP_HOC lh in db.LOP_HOCs)
            {
                if (lh.MALOPHOC == maLopHoc)
                {
                    lopHocID = lh.LOPHOCID;
                }
            }
            var donPhucKhaos = db.DON_PHUC_KHAOs.Where(temp => temp.HOCKY.Equals(hocKy) && temp.NAM.Equals(nam) && temp.LOPHOCID.Equals(lopHocID) && temp.HINHTHUCPHUCKHAO.Contains(hinhThucPhucKhao));

            foreach (DON_PHUC_KHAO d in donPhucKhaos)
            {
                DON_PHUC_KHAO dp = new DON_PHUC_KHAO { DONPHUCKHAOID = d.DONPHUCKHAOID, SINHVIENID = d.SINHVIENID, HINHTHUCPHUCKHAO = d.HINHTHUCPHUCKHAO, NGAYPHUCKHAO = d.NGAYPHUCKHAO, HOCKY = d.HOCKY, NAM = d.NAM, LOPHOCID = d.LOPHOCID};
                ds.Add(dp);
            }
            return ds;
        }
    }
}
